
package com.kugou.game.sdk.ui.dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kugou.game.sdk.utils.Rx;

public class SelectOptionDialog extends Dialog {

    public interface OnSelectOptionListener {
        /**
         * 在用户账号选择对话框中选择某个用户账号
         * 
         * @param userData
         */
        public void afterSelectUser(String option, int index);

    }

    private LayoutInflater mInflater;

    private OnSelectOptionListener mOnSelectOptionListener;

    private ListView mListView;

    private Activity mContext;

    private TextView mTitle;

    private OptionAdapter mAdapter;

    private ArrayList<String> mOptions = new ArrayList<String>();

    private int mCurrentOptionIndex = -1;

    public SelectOptionDialog(Activity context, int theme) {
        super(context, theme);
        OptionAdapter adapter;
        ListView listView;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        this.mContext = context;
        initView();
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }

    public SelectOptionDialog(Activity activity) {
        this(activity, 0);
    }

    private void initView() {
        int layoutId = Rx.layout.kg_recharge_select_dialog;
        setContentView(layoutId);
    }

    public void setDialogDefaultSize() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int parentWidth = dm.widthPixels;
        int parentHeight = dm.heightPixels;

        lp.height = (int) ((mOptions.size() + 1) * 50 * dm.density);

        if (parentWidth < parentHeight) {// 竖屏
            lp.width = (int) (parentWidth * 0.9);
        } else {
            if (mOptions.size() > 5) {
                lp.height = (int) (300 * dm.density);
            }
            lp.width = (int) (parentHeight * 0.9);
        }
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        mListView = (ListView) findViewById(Rx.id.kg_lv_select_option);
        if (mListView == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_all_user_list'");
        }

        mAdapter = new OptionAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mOnSelectOptionListener != null) {
                    mOnSelectOptionListener.afterSelectUser(mOptions.get(position), position);
                }
                dismiss();
            }
        });

        mTitle = (TextView) findViewById(Rx.id.kg_tv_dialog_title);
    }

    public void setCurrentOptionIndex(int index) {
        mCurrentOptionIndex = index;
    }

    /**
     * 设置对话框标题
     */
    public void setDialogTitle(String str) {
        mTitle.setText(str);
    }

    public void notifyAdapterChage() {
        mAdapter.notifyDataSetInvalidated();
    }

    public void addDatas(ArrayList<String> options) {
        if (options != null && options.size() > 0) {
            mOptions.addAll(options);
        }
    }

    public void clearDatas() {
        if (mOptions != null) {
            mOptions.clear();
        }
    }

    public void setOnSelectOptionListener(OnSelectOptionListener listener) {
        this.mOnSelectOptionListener = listener;
    }

    class OptionAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mOptions.size();
        }

        @Override
        public Object getItem(int position) {
            return mOptions.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final int index = position;
            if (convertView == null) {
                convertView = mInflater.inflate(Rx.layout.kg_recharge_option_item, null);
            }

            TextView tView = (TextView) convertView.findViewById(Rx.id.kg_tv_recharge_option);

            tView.setText(mOptions.get(position));

            ImageView btn = (ImageView) convertView.findViewById(Rx.id.kg_ib_recharge_option);
            if (index == mCurrentOptionIndex) {
                btn.setImageResource(Rx.drawable.kg_option_selected);
            } else {
                btn.setImageResource(Rx.drawable.kg_option_normal);
            }

            return convertView;
        }
    }

}
